define(["dojo/_base/array",
  "esri/symbols/SimpleMarkerSymbol",
  "esri/symbols/SimpleLineSymbol",
	"esri/symbols/SimpleFillSymbol",
  "esri/symbols/CartographicLineSymbol",
	"esri/graphic","esri/geometry/Point",
	"esri/tasks/GeometryService",
  "esri/tasks/ProjectParameters",
  "esri/SpatialReference",
	"esri/Color","dojo/_base/lang",
  "esri/symbols/PictureFillSymbol",
  "esri/symbols/PictureMarkerSymbol"
], function(array, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, CartographicLineSymbol,Graphic,Point,GeometryService,ProjectParameters, SpatialReference,Color,lang,PictureFillSymbol,PictureMarkerSymbol) {
	var markerSymbol = new SimpleMarkerSymbol();
	markerSymbol.setPath("M16,4.938c-7.732,0-14,4.701-14,10.5c0,1.981,0.741,3.833,2.016,5.414L2,25.272l5.613-1.44c2.339,1.316,5.237,2.106,8.387,2.106c7.732,0,14-4.701,14-10.5S23.732,4.938,16,4.938zM16.868,21.375h-1.969v-1.889h1.969V21.375zM16.772,18.094h-1.777l-0.176-8.083h2.113L16.772,18.094z");
	markerSymbol.setColor(new Color("#00FFFF"));
	var lineSymbol = new CartographicLineSymbol(
		CartographicLineSymbol.STYLE_SOLID,
		new Color([255, 0, 0]), 10,
		CartographicLineSymbol.CAP_ROUND,
		CartographicLineSymbol.JOIN_MITER, 5
	);
	var fillSymbol = new SimpleFillSymbol(
		SimpleFillSymbol.STYLE_SOLID,
		new SimpleLineSymbol(
			SimpleLineSymbol.STYLE_SOLID,
			new Color([ 0,255,240]),
			1
		),
		new Color([ 0,252,255,0.3])
	);
	var highLightFillSymbol = new SimpleFillSymbol(
		SimpleFillSymbol.STYLE_SOLID,
		new SimpleLineSymbol(
			SimpleLineSymbol.STYLE_SOLID,
			//new Color([255,153,153, 0.35]),
			new Color([ 255,32,96]),
			2
		),
		new Color([255,35,97,0.3])
	);
	var geometryService=new GeometryService(dojoConfig.layers["geometryServer"].url);
	return {
		addGraphics: function(featureSet, graphicsLayer,cutgeometry) {
			var self = this;
			if(cutgeometry && featureSet.features){//是否需要切割查询结果，只显示多边形查询内部的结果
				self.cutFeatures(graphicsLayer,featureSet,cutgeometry,function(features){
					array.forEach(features, function(feature) {
							graphicsLayer.add(feature);
					});
				});
			}else{
				array.forEach(featureSet.features, function(feature) {
					var symbol = self.getSymbol(feature.geometry);
					if(symbol != null){
						feature.setSymbol(symbol);
						graphicsLayer.add(feature);
					}
				});
			}
		},
		/**
		 * 修改矢量后让数据从新显示
		 */
		updGraphic:function(graphic,graphicsLayer){
			var self =  this;
			var graphics = graphicsLayer.graphics;
			for (var i = 0; i < graphics.length; i++) {
				if(graphic.attributes.OBJECTID === graphics[i].attributes.OBJECTID){
					graphicsLayer.remove(graphics[i]);
					break;
				}
			}
			var symbol = self.getSymbol(graphic.geometry,true);
			graphic.setSymbol(symbol);
			graphicsLayer.add(graphic);
		},
		/**
		 * @summary 根据geometry获取默认样式
		 * @param {Geometry} geometry
		 * @param {bool} flag  为true时，获取高亮样式
		 */
		getSymbol: function(geometry, flag) {
			var symbol = null;
			if(geometry != null){
			if(geometry.type === "point" || geometry.type === "multipoint") {
				symbol = flag ? markerSymbol : markerSymbol;
			} else if(geometry.type === "line" || geometry.type === "polyline") {
				symbol = flag ? lineSymbol : lineSymbol;
			} else {
				symbol = flag ? highLightFillSymbol : fillSymbol;
			}
			}
			return symbol;
		},
		getPictureSymbol: function(geometry, params, flag) {
			var symbol;
			if(geometry.type === "point" || geometry.type === "multipoint") {
				symbol = flag ? markerSymbol : markerSymbol;
			} else if(geometry.type === "line" || geometry.type === "polyline") {
				symbol = flag ? lineSymbol : lineSymbol;
			} else {
				if(params&&params.url) {
					symbol = createPictureMarkerSymbol(params);
				} else {
					symbol = flag ? highLightFillSymbol : fillSymbol;
				}
			}
			return symbol;
		},
		parseGraphic:function(geometry){
			var symbol=this.getSymbol(geometry);
			return new Graphic(geometry,symbol);
		},
		/**
		 * @summary 设置graphic的样式
		 * @param {Graphic} graphic
		 * @param {bool} flag 为true时，设置高亮样式
		 */
		setSymbol: function(graphic, flag) {
			var symbol = this.getSymbol(graphic.geometry, flag);
			graphic.setSymbol(symbol);
		},
		setExtent:function(graphic,map){
			var extent=graphic.geometry.getExtent();
			map.setExtent(extent);
		},
		/**获取高亮样式在原来样式的基础上修改**/
		getHighLightFillSymbol:function(pictureSymbol){
			if(pictureSymbol){
				pictureSymbol.outline= new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 255, 255,1]), 1);
			}
			return pictureSymbol;
		},
		/**去掉高亮样式在原来样式的基础上修改**/
		removeHighLightFillSymbol:function(pictureSymbol){
			if(pictureSymbol){
				pictureSymbol.outline= new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0,102,0, 0.5]), 1);
			}
			return pictureSymbol;
		},
		/**
		 *
		 * @param {Array<Geometry>} geometries
		 * @param {String} wkid
		 */
		reSetSpatialReference:function(geometries,wkid){
			var projectParameters=new ProjectParameters();
			projectParameters.geometries =geometries;
			var spatialReference=new SpatialReference(wkid);
            projectParameters.outSR = spatialReference;
			return geometryService.project(projectParameters);
		},
		/**
		 *
		 * @param {number} x
		 * @param {number} y
		 * @param {number} wkid
		 */
		createPoint:function(x,y,wkid){
			return new Point(x,y, new SpatialReference({ wkid: wkid }));
		},
		createPictureFillSymbol : function(params) {
			var options = {
				/*"url": "graphics/redArrow2.png",*/
				"height": 40,
				"width": 40,
				/*color:new Color([0,102,0,0.35]),*/
				outline: new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 102, 0, 0.35]), 1),
				"type": "picturefillsymbol"
			};
			var url = require.toUrl(params.url);
			lang.mixin(options, params, {
				url: url
			});
			var ins = new PictureFillSymbol();
			ins.height =params.height?params.height:options.height;;
			/*ins.color=options.color;*/
			ins.width = params.width?params.width:options.width;
			ins.url = options.url;
			ins.outline = options.outline;
			ins.type = options.type;
			ins.offset(0,ins.height/2);
			return ins;
		},
		createPictureMarkerSymbol:function(params){
			var options = {
				"height": 20,
				"width": 20,
				"type": "esriPMS"
				};
				var url = require.toUrl(params.url);
				lang.mixin(options, params, {
					url: url
				});
				var ins = new PictureMarkerSymbol();
				ins.height = options.height;
				ins.height = options.width;
				ins.url = options.url;
				ins.outline = options.outline;
				ins.type = options.type;
				ins.yoffset = options.height/2;
				return ins;
		},
		//每次的symbol颜色不一样时返回新的symbol 不然颜色会串
		fillSymbleGraphics:function(geometry){
			var symbol = new SimpleFillSymbol(
					SimpleFillSymbol.STYLE_SOLID,
					new SimpleLineSymbol(
						SimpleLineSymbol.STYLE_SOLID,
						new Color([0,102,0, 0.35]),
						1
					),
					new Color([0,102,0, 0.35])
				);
			return new Graphic(geometry,symbol);
		},
		//返回森林小班样式
		returnXiaobanWP:function(){
				 var symbol = new SimpleFillSymbol(
						SimpleFillSymbol.STYLE_SOLID,
						new SimpleLineSymbol(
						SimpleLineSymbol.STYLE_SOLID,
						 new Color([182,182,182, 0.7]),
						1
					),
					new Color([227,249,231, 0.35]));

				 return symbol;
		},
		//返回删除的样式
		returnDelSymbol:function(){
			/* new SimpleFillSymbol(
						SimpleFillSymbol.STYLE_SOLID,
						new SimpleLineSymbol(
							SimpleLineSymbol.STYLE_SOLID,
							new Color([ 0,255,240]),
							1
						),
						new Color([ 0,252,255,0.3])
					);*/
			 var symbol =  new SimpleFillSymbol(
						SimpleFillSymbol.STYLE_SOLID,
						new SimpleLineSymbol(
						SimpleLineSymbol.STYLE_SOLID,
						new Color([191,187,187, 1]),
						1
					),
					new Color([191,187,187, 0.5]));

			 return symbol;
		},
		cutFeatures:function(graphicsLayer,featureSet,geometry,callback){
			var self = this;
			var sets =[];
			var i =0;
			var attributes = [];
			var geometries=[];
			graphicsLayer.clear();
			array.forEach(featureSet.features, function(feature) {
				attributes.push(feature.attributes);
				geometries.push(feature.geometry);
			});
			//第一个参数是图形集合，第二个参数是一个图形
			geometryService.intersect(geometries,geometry,function(result){
				//创建Graphic
				for(var i=0 ;i<result.length;i++){
			    	for (var j = 0; j < featureSet.features.length; j++) {
						if(attributes[i].OBJECTID == featureSet.features[j].attributes.OBJECTID){
							var symbol = self.getSymbol(result[i]);
							featureSet.features[j].geometry = result[i];
							featureSet.features[j].setSymbol(symbol);
							if( i== featureSet.features.length-1 && typeof callback  =="function"){
								callback(featureSet.features);
							}
						}
					}
				}
			})

		}

	}

});
